package com.simpd.activity;

import cn.pedant.SweetAlert.SweetAlertDialog;

import com.example.simpd.R;
import com.simpd.db_helper.DatabaseHelper;
import com.simpd.model.ServerSimpd;
import com.simpd.model.SynchronizeConstant;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class SettingActivity extends Activity {
	
	RelativeLayout groupButton; 
	RelativeLayout groupInput; 
	
	Button btnEdit; 
	Button btnResetDefault; 
	Button btnSave;
	Button btnCancel; 
	
	EditText inputUrl;
	TextView textUrl; 
	
	private DatabaseHelper db; 
	private ServerSimpd server;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		getActionBar().hide();
		super.onCreate(savedInstanceState);
		
		Configuration configuration = this.getResources().getConfiguration(); 
		DisplayMetrics metrics = this.getResources().getDisplayMetrics(); 
		
		if(configuration.orientation == Configuration.ORIENTATION_LANDSCAPE 
		   && metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM){
			setContentView(R.layout.setting_tablet_activity); 
			
			groupButton = (RelativeLayout) findViewById(R.id.setting_tablet_rellayout_group_button);
			groupInput  = (RelativeLayout) findViewById(R.id.setting_tablet_rellayout_group_input);
			
			btnEdit			= (Button) findViewById(R.id.setting_tablet_button_edit);
			btnResetDefault = (Button) findViewById(R.id.setting_tablet_button_reset_default_url);
			btnSave			= (Button) findViewById(R.id.setting_tablet_button_simpan); 
			btnCancel		= (Button) findViewById(R.id.setting_tablet_button_cancel);
			
			inputUrl		= (EditText) findViewById(R.id.setting_tablet_input_url); 
			textUrl			= (TextView) findViewById(R.id.setting_tablet_text_active_url);

		}else{
			setContentView(R.layout.setting_activity);
			
			groupButton = (RelativeLayout) findViewById(R.id.setting_rellayout_group_button);
			groupInput  = (RelativeLayout) findViewById(R.id.setting_rellayout_group_input);
			
			btnEdit			= (Button) findViewById(R.id.setting_button_edit);
			btnResetDefault = (Button) findViewById(R.id.setting_button_reset_default_url);
			btnSave			= (Button) findViewById(R.id.setting_button_simpan); 
			btnCancel		= (Button) findViewById(R.id.setting_button_cancel);
			
			inputUrl		= (EditText) findViewById(R.id.setting_input_url); 
			textUrl			= (TextView) findViewById(R.id.setting_text_active_url);
		}
		
		
		
		
		
		db = new DatabaseHelper(this);
		server = db.getServer(); 
		
		if(server == null){
			server = new ServerSimpd(); 
			server.setServerUrl(SynchronizeConstant.DEFAULT_SERVER_SIMPD); 
			db.insertServer(server);
		}
		
		
		textUrl.setText(server.getServerUrl());
		
		btnEdit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				groupButton.setVisibility(View.GONE);
				groupInput.setVisibility(View.VISIBLE);
				server = db.getServer(); 
				inputUrl.setText(""+server.getServerUrl());
			}
		});
		
		btnSave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String strUrl = inputUrl.getText().toString();
				SweetAlertDialog alert = null;
				if(strUrl.length()>0 && (strUrl !="" && !strUrl.contains(" "))){
					
					if(strUrl.charAt(strUrl.length()-1) == '/'){
						strUrl = strUrl.substring(0, strUrl.length()-1);
					}
					if((strUrl.startsWith("http://") == false) && strUrl.startsWith("https://")==false){
						strUrl = "http://"+strUrl; 
					}
					server.setServerUrl(strUrl);
					db.updateServer(server);
					textUrl.setText(""+server.getServerUrl());
					
					groupInput.setVisibility(View.GONE); 
					groupButton.setVisibility(View.VISIBLE);
					alert = new SweetAlertDialog(SettingActivity.this, SweetAlertDialog.SUCCESS_TYPE);
					alert.setTitleText("Selamat!");
					alert.setContentText("Server URL berhasil diganti menjadi : \n"+server.getServerUrl());
					alert.show();
				}else{
					alert = new SweetAlertDialog(SettingActivity.this, SweetAlertDialog.ERROR_TYPE);
					alert.setTitleText("Opps..!");
					alert.setContentText("URL baru tidak berhasil disimpan karena kosong atau mengandung spasi!");
					alert.show();
				}
			}
		});
		
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				inputUrl.setText("");
				groupInput.setVisibility(View.GONE); 
				groupButton.setVisibility(View.VISIBLE);
			}
		});
		
		btnResetDefault.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				server = db.getServer(); 
				server.setServerUrl(SynchronizeConstant.DEFAULT_SERVER_SIMPD);
				db.updateServer(server);
				SweetAlertDialog alert = new SweetAlertDialog(SettingActivity.this, SweetAlertDialog.SUCCESS_TYPE);
				alert.setTitleText("Selamat!");
				alert.setContentText("Server URL berhasil diganti menjadi : \n"+server.getServerUrl());
				alert.show();
				
				textUrl.setText(""+server.getServerUrl());
			}
		});
	}
}
